package com.source.zookeeper;

import org.apache.zookeeper.*;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class ZooKTest2 {
    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        CountDownLatch countDownLatch = new CountDownLatch(1);

      /*  ZooKeeper zookeeper = new ZooKeeper("192.168.234.131:2181", 5000, (WatchedEvent x) -> {
            if (x.getState() == Watcher.Event.KeeperState.SyncConnected) {
                System.out.println("连接成功");
                countDownLatch.countDown();
            }
        });*/

        ZooKeeper zookeeper = new ZooKeeper("192.168.234.131:2181", 5000, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                if(watchedEvent.getState()== Event.KeeperState.SyncConnected){
                    System.out.println("连接成功11");
                    System.out.println(watchedEvent.getState().getIntValue());
                    System.out.println("连接成功22");

                }
            }
        });

        countDownLatch.await();
        System.out.println(zookeeper.getSessionId());

        String s = zookeeper.create("/zk11", "weng".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println("result:"+s);





        zookeeper.close();
    }
}
